package com.mxf.code.service;

import com.mxf.code.dao.AccountDao;
import com.mxf.code.entity.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author : [mxf]
 * @version : [v1.0]
 * @className : TransationServiceImpl
 * @description : [描述说明该类的功能]
 * @createTime : [2022/9/11 15:24]
 * @updateUser : [mxf]
 * @updateTime : [2022/9/11 15:24]
 * @updateRemark : [描述说明本次修改内容]
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public void doTransfer(int from, int to, BigDecimal bigDecimal) throws IOException {
        Account fromAccount = accountDao.selectByPrimaryKey(from);
        fromAccount.setMoney(fromAccount.getMoney().subtract(bigDecimal));
        accountDao.updateByPrimaryKeySelective(fromAccount);

        /*if (1==1){
            throw new IOException("ddd");
        }*/
        Account toAccount = accountDao.selectByPrimaryKey(to);
        toAccount.setMoney(toAccount.getMoney().add(bigDecimal));
        accountDao.updateByPrimaryKeySelective(toAccount);
    }
}
